<template>
    <div class="app-container">
        <el-row :gutter="20">
            <!--数据-->
            <el-col :span="4" :xs="24">
                <!-- <div class="head-container">
                    <el-input v-model="deptName" placeholder="请输入" clearable size="small" prefix-icon="el-icon-search" style="margin-bottom: 20px" />
                </div> -->
                <div class="head-container">
                    <el-tree :data="deptOptions" :props="defaultProps" :expand-on-click-node="false" :filter-node-method="filterNode" ref="tree" node-key="id" default-expand-all highlight-current @node-click="handleNodeClick" />
                </div>
            </el-col>
            <!--数据-->
            <el-col :span="20" :xs="24">
                <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
                    <el-form-item :label="$locale('jobList.add.Student Name')" prop="studentName">
                        <el-input v-model="queryParams.studentName" :placeholder="$locale('jobList.add.Please enter the student name')" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{ $locale('geWudevice.add.search for') }}</el-button>
                        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery(ids)">{{ $locale('geWudevice.reset') }}</el-button>
                    </el-form-item>
                </el-form>

                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">{{ $locale('myExperiment.studentListDialog.submit') }}</el-button>
                    </el-col>
                </el-row>

                <el-table v-loading="loading" ref="table" :data="studentList" @select="handleSelectionChange" @select-all="selectAll">
                    <el-table-column type="selection" width="55">
                    </el-table-column>
                    <el-table-column :label="$locale('student.student')" align="center" key="studentName" prop="studentName">
                    </el-table-column>
                    <el-table-column :label="$locale('jobList.add.email')" align="center" key="contactEmail" prop="contactEmail" width="160">
                    </el-table-column>
                    <el-table-column :label="$locale('myClass.add.School Name')" align="center" key="schoolName" prop="schoolName">
                    </el-table-column>
                    <el-table-column :label="$locale('classList.add.class name')" align="center" key="className" prop="className">
                    </el-table-column>
                    <el-table-column :label="$locale('classList.add.Teacher in charge')" align="center" key="teacherName" prop="teacherName">
                    </el-table-column>
                    <el-table-column sortable column-key="status" :filters="[{text: $locale('classList.normal'), value: '1'}, {text: $locale('classList.freeze'), value: '0'}]" :label="$locale('geWudevice.add.state')" align="center" key="status" prop="status">
                        <template slot-scope="scope">
                            <span v-for="item in dict.type.data_status">
                                <el-tag v-if="item.value == scope.row.status" :type="scope.row.status == 0?'success':'danger'" effect="dark">
                                    {{ item.label }}
                                </el-tag>
                            </span>
                        </template>
                    </el-table-column>
                    <el-table-column :label="$locale('student.Facial recognition frequency')" align="center" key="facialNum" prop="facialNum">
                    </el-table-column>
                    <el-table-column :label="$locale('myExperiment.studentListDialog.Is there an equipment inventory')" align="center" key="checkDevice" prop="checkDevice">
                        <template slot-scope="scope">
                            {{ scope.row.checkDevice?$locale('geWuExperiment.yes'):$locale('geWuExperiment.no') }}
                        </template>
                    </el-table-column>
                    <el-table-column :label="$locale('myExperiment.studentListDialog.Whether to submit beyond the deadline')" align="center" key="overSubmit" prop="overSubmit">
                        <template slot-scope="scope">
                            {{ scope.row.overSubmit ? $locale('geWuExperiment.yes') : $locale('geWuExperiment.no') }}
                        </template>
                    </el-table-column>
                </el-table>

                <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
            </el-col>
        </el-row>
    </div>
</template>

<script>
import { studentPageV2 } from "@/api/myClass/student";
import { getClassList } from "@/api/myClass/classList";

export default {
    name: "student",
    dicts: ['data_status'],
    data () {
        return {
            // 遮罩层
            loading: false,
            // 选中数组
            ids: [],
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 用户表格数据
            studentList: [],
            // 弹出层标题
            title: "",
            loginForm: {
                id: '',
                password: '',
                oldPassword: ''
            },
            // 部门树选项
            deptOptions: undefined,
            // 是否显示弹出层
            open: false,
            // 部门名称
            deptName: undefined,
            // 表单参数
            defaultProps: {
                children: 'zones',
                isLeaf: 'leaf',
                label: "name"
            },
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10
            },
            // 表单校验
            rules: {

            }
        };
    },
    watch: {
        // 根据名称筛选部门树
        deptName (val) {
            this.$refs.tree.filter(val);
        }
    },
    created () {

    },
    computed: {
        userName () {
            return this.$store.getters.name; // 使用 Vuex 的 getter 获取用户姓名
        }
    },
    methods: {
        /** 查询用户列表 */
        getList () {
            this.loading = true;
            studentPageV2(this.queryParams).then(({ data }) => {
                this.studentList = data.records;
                this.total = data.total;
                this.loading = false;
                this.$nextTick(() => {
                    console.log(this.ids, 2)
                    this.selectRowsById(this.ids.map(item => item.id)); // 保留原选中的数据
                })
            });
        },
        /** 查询部门下拉树结构 */
        getDeptTree () {
            getClassList().then(response => {
                this.deptOptions = response.data.map(item => {
                    item['name'] = item.className;
                    return item;
                });

            })
        },
        /** 修改按钮操作 */
        handleResetting (row) {
            this.resetForm("loginForm");
            this.loginForm.id = row.id
            this.open = true
        },
        handleExperiment (row) {
            this.$refs.experiment.show(row)
        },
        /** 新增按钮操作 */
        handleAdd () {
            this.$emit('addStudentList', this.ids)
        },
        /** 修改按钮操作 */
        handleUpdate (row, disabled) {
            this.$refs.add.handleUpdate(row, disabled)
        },
        // 筛选节点
        filterNode (value, data) {
            if (!value) return true;
            return data.label.indexOf(value) !== -1;
        },
        // 节点单击事件
        handleNodeClick (data) {
            if (data.className) {
                this.queryParams.classId = [data.id];
                this.handleQuery();
            }
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs["loginForm"].validate(valid => {
                if (valid) {
                    resetPwd(this.loginForm).then(response => {
                        this.$modal.msgSuccess(this.$locale('geWudevice.add.tip5'));
                        this.getList();
                        this.open = false;
                    });
                }
            });
        },
        /** 搜索按钮操作 */
        handleQuery () {
            this.queryParams.pageNum = 1;
            this.getList();

        },
        /** 重置按钮操作 */
        resetQuery (studentList) {
            this.ids = studentList
            this.resetForm("queryForm");
            this.queryParams.classId = undefined;
            this.$refs.tree.setCurrentKey(null);
            this.getDeptTree();
            this.handleQuery();
        },
        handleSelectionChange (selection, row) {
            const rowId = row.id;
            const index = this.ids.findIndex(item => item.id === rowId);
            if (index === -1) {
                this.ids.push(row);
            } else {
                this.ids.splice(index, 1);
            }
            console.log(row, this.ids, 3)
        },
        selectRowsById (ids) {
            console.log(ids, 1111)
            const table = this.$refs.table;
            if (!table) return;
            table.clearSelection(); // 清除所有选中行
            this.studentList.forEach(row => {
                if (ids.includes(row.id)) {
                    table.toggleRowSelection(row, true); // 根据 ID 选择对应的行
                }
            });
        },
        selectAll (selection, row) {
            console.log(selection, row)
            // 获取所有学生的 ID
            const allIds = this.studentList.map(student => student.id);
            this.ids = [...allIds];
        },
        deselectAll () {
            this.ids = [];
            console.log('All students deselected:', this.ids);
        }
    }
};
  </script>
<style lang="scss" scoped>
::v-deep .el-table__row--level-1 .el-checkbox {
    display: none;
}
::v-deep .el-table__header-wrapper .el-checkbox {
    display: none;
}
</style>
